<?php
class ScriptsController extends Zendvn_Controller_Action{
	private $_paning = array('rowCount' => 20, 'pageRange' => 5);
	public function init(){
		parent::init();		
		$this->_templateObj->setPath(TEMPLATE_PATH . '/public/default');		
		$this->_templateObj->load();	
		
		/*====================================================
		 * Load Zendvn Translate for Controller
		 *====================================================*/
		$this->_langObj->setLangFile(array('validate.tmx'));		
		Zend_Registry::set('Zend_Translate',$this->_langObj->generate());
		
		/*====================================================
		 * Thiet lap gia tri cho phan phan trang cua controlle
		 *====================================================*/
		$pageNumber = $this->_request->getParam('page',1);
		$this->_paning['page'] = $pageNumber;
		$this->_arrParam['panigator'] = $this->_paning;
		$this->view->arrParam = $this->_arrParam;
	}
	
	public function createImageAction(){
		require_once SCRIPTS_PATH . '/resize/resize.php';
		
		$name     = base64_decode($this->_arrParam['name']);
		
		$width    = $this->_arrParam['width'];
		$heigh    = $this->_arrParam['heigh'];
		$type     = $this->_arrParam['type'];
		$resize = new resize($name);
		$resize->resizeImage($width, $heigh, $type);
		$resize->showImage();
		
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
	}
	
	public function getContentUrlAction(){
		$crawler		= new Zendvn_CrawlContent();
		$tblItem		= new Zendvn_Model_Table_LinkContent();
		$tblNews		= new Zendvn_Model_Table_NewsItem();
		/*================================
		 * Process Domain
		 *================================*/
		$url = urldecode($this->_arrParam['url']);
		preg_match('/^((http|https):\/\/)?([^\/]+)/i', $url, $domain);

		if(empty($domain[1])){
			$domain = 'http://' . $domain[2] . '/';
		}else{
			$domain = $domain[0] . '/';
		}
		/*================================
		 * Get Content
	 	*================================*/
	 	$result = $tblItem->getItem($this->_arrParam, array('task' => 'get-content-item'));
		if(!empty($result)){
			$this->view->Item = array();
			$this->view->Item['title'] 			= $result['title'];
			$this->view->Item['description'] 	= $result['description'];
			$this->view->Item['domain'] 		= $result['domain'];
			$this->view->Item['images'] 		= json_decode($result['images'], true);
		}else{
			$this->view->Item = $crawler->getPageInfo($url, $domain);
			$this->view->Item['id_item'] = $this->_arrParam['id_item'];
			$tblItem->saveItem($this->view->Item);
			echo '<h4>Data is updating...</h4>';
			$this->_helper->viewRenderer->setNoRender();
		}
		/*================================
		 * Update View
		 *================================*/
		 $tblNews->update(array('view' => new Zend_Db_Expr('view + 1'),
		 						'recent' => date('Y-m-d H:i:s')
		 						), 'id = ' . $this->_arrParam['id_item']);
		 
		 
		$this->_helper->layout->disableLayout();
	}
}